# poetry controller
from app.core.crud import CRUDBase
from app.models.poetry import Poetry
from app.schemas.poetry import PoetryCreate, PoetryUpdate


class PoetryController(CRUDBase[Poetry, PoetryCreate, PoetryUpdate]):
    def __init__(self):
        super().__init__(model=Poetry)

    async def is_exist(self, name: str) -> bool:
        return await self.model.filter(name=name).exists()

    async def update_Poetry(self, id: int, obj_in: PoetryUpdate):
        poetry = await self.model.get(id=id)
        poetry.title = obj_in.title
        poetry.rhythmic = obj_in.rhythmic
        poetry.categoryCode = obj_in.categoryCode
        poetry.desc = obj_in.desc
        poetry.author = obj_in.author
        poetry.paragraphs = obj_in.paragraphs
        poetry.notes = obj_in.notes
        poetry.section = obj_in.section
        await Poetry.save()

    async def create_bulk(self, obj_list: list[PoetryCreate]):
        await self.model.bulk_create([self.model(**obj.dict()) for obj in obj_list])


poetry_controller = PoetryController()
